<?php
use app\models\Mall;
use app\widgets\MallMBottomToolbarWidget;
use yii\helpers\Url;

$web = Url::base();
$institution_code = Mall::getCurrentInstitutionCode();
$integral_unit = \app\models\Mall::getCurrentInstitutionIntegralUnit();
$site_name = \app\models\Mall::getCurrentInstitutionSiteName();
$this->title = $site_name . '-购物车';
?>


<link rel="stylesheet" type="text/css" href="<?= $web ?>/css/mall-m/cart.css"/>
<link rel="stylesheet" type="text/css" href="<?= $web ?>/css/mall-m/cross-title.css"/>

<style type="text/css">
    .content .content-text .content-text-1 .choice-checkbox:checked {
        background: url(<?= $web ?>/images/mall-m/pitch-1.png) no-repeat;
        background-size: 100% 100%;
    }

    .content .content-text .content-text-1 .choice-checkbox {
        background: url(<?= $web ?>/images/mall-m/choice-1.png) no-repeat;
        background-size: 100% 100%;
    }

    .content .content-head .choice-checkbox {
        background: url(<?= $web ?>/images/mall-m/choice-1.png) no-repeat;
        background-size: 100% 100%;
    }

    .content .content-head .choice-checkbox:checked {
        background: url(<?= $web ?>/images/mall-m/pitch-1.png) no-repeat;
        background-size: 100% 100%;
    }
</style>

<div class="main">
    <?php
    if ($isLogin) {
        if (count($cartList) > 0) {
            ?>
            <div class="content" style="display: block;">
                <div class="content-head">
                    <label class="checkbox f-l"><input id="all_checkbox" class="choice-checkbox" type="checkbox">全选</label>
                    <a href="javascript:removeCartGoods()">
                        <div class="btn-2 f-r">
                            <p>删除</p>
                        </div>
                    </a>
                </div>
                <div class="content-text">
                    <?php

                    foreach ($cartList as $item) {
                        $isSelected = '';
                        if ($item['is_selected'] == 1) {
                            $isSelected = 'checked="true"';
                        }
                        ?>
                        <div class="content-text-1" cartId="<?= $item['id'] ?>">
                            <label class="checkbox f-l"><input class="choice-checkbox" type="checkbox" <?= $isSelected ?>/></label>
                            <a href="goods-detail?goods_code=<?= $item['goods_code'] ?>&institution_code=<?= $institution_code ?>">
                                <div class="content-img f-l">
                                    <img src="<?= $item['goods_image'] ?>@120w"/>
                                </div>
                            </a>
                            <div class="content-text-1-p f-r">
                                <p class="crossborder">
                                    <?php
                                    if($item['is_crossborder']=='1'){
                                        ?>
                                        <span>跨境</span>
                                        <?php
                                    }
                                    ?>
                                    <?= $item['name'] ?></p>

                                <div class="goods-number">
                                    <div class="gw-num f-l" stock="<?= $item['stock'] ?>">
                                        <span class="jian" style="color: <?= $item['quantity'] <= 1 ? '#E2E2E2' : '#8a8a8a' ?>;">-</span>
                                        <input type="text" value="<?= $item['quantity'] ?>" class="num"/>
                                        <span class="add" style="color: <?= $item['quantity'] >= $item['stock'] ? '#E2E2E2' : '#8a8a8a' ?>;">+</span>
                                    </div>

                                    <div class="goods-price f-r">
                                        <p>单价：<span><span name="goods_price"><?= intval($item['selling_integral']) ?></span><?= $integral_unit ?></span></p>
                                    </div>
                                </div>
                            </div>

                        </div>
                        <?php
                    }
                    ?>

                    <!--<div class="content-text-1">
                        <label class="checkbox f-l"><input class="choice-checkbox" type="checkbox"></label>
                        <a href="">
                            <div class="content-img f-l">
                                <img src="<?/*= $web */
                    ?>/images/mall-m/order-from-content1-img.png"/>
                            </div>
                        </a>
                        <div class="content-text-1-p f-r">
                            <p>女神范显高勾搭术 拍下立减5元 修身身约身约会必备气质长款小黑包包.......</p>

                            <div class="goods-number">
                                <div class="gw-num f-l">
                                    <span class="jian">-</span>
                                    <input type="text" value="1" class="num"/>
                                    <span class="add">+</span>
                                </div>

                                <div class="goods-price f-r">
                                    <p>单价：<span>99积分</span></p>
                                </div>
                            </div>
                        </div>

                    </div>-->


                    <script type="text/javascript">
                        function SetCartQuantity(cartId, quantity) {
                            var data = {};
                            data.cartId = cartId;
                            data.quantity = quantity;
                            requestJsonMallM('set-cart-quantity-handle', data);
                        }
                        $(function () {
                            $(".add").click(function () {
                                var stock = $(this).closest('.gw-num').attr('stock');
                                var n = $(this).prev().val();
                                var num = parseInt(n) + 1;
                                if (num == 0) {
                                    return;
                                }
                                if (num > 1) {
                                    $(this).siblings(".jian").css("color", "#8a8a8a");
                                }
                                if (num > stock) {
                                    $(this).css("color", "#E2E2E2");
                                    return;
                                }
                                SetCartQuantity($(this).closest('.content-text-1').attr('cartId'), num);
                                $(this).prev().val(num);
                                calcTotal();
                            });
                            $(".jian").click(function () {
                                var stock = $(this).closest('.gw-num').attr('stock');
                                var n = $(this).next().val();
                                var num = parseInt(n) - 1;
                                if (num == 0) {
                                    return;
                                }
                                if (num == 1) {
                                    $(this).css("color", "#E2E2E2");
                                }
                                if (num < stock) {
                                    $(this).siblings(".add").css("color", "#8a8a8a");
                                }
                                $(this).next().val(num);
                                SetCartQuantity($(this).closest('.content-text-1').attr('cartId'), num);
                                calcTotal();
                            });
                        });
                    </script>
                </div>

                <div class="balance">
                    <div class="total f-r">
                        <p>合计：<span><span name="totalPrice">--</span><?= $integral_unit ?></span></p>
                    </div>

                </div>
                <a href="javascript:submit_order()">
                    <div class="affirm-btn">
                        <p>提交订单</p>
                    </div>
                </a>
            </div>
            <?php
        } else {
            ?>
            <div class="page-prompt-4">
                <img class="page-prompt" src="<?= $web ?>/images/mall-m/page-prompt-4.png"/>
                <p class="title">购物车空空如也，赶紧去逛逛</p>
<!--                <a href="category">-->
<!--                    <div class="skip-btn">-->
<!--                        开始选逛-->
<!--                    </div>-->
<!--                </a>-->
                <div class="content-2">
                <div class="link-btn f-l">
                    <a href="<?= Url::to(['mall-m/category', 'institution_code' => Mall::getCurrentInstitutionCode()]) ?>">
                        <div class="stroll f-l">
                            <img class="f-l" src="<?= $web ?>/images/mall-m/stroll-icon.png"/>
                            <p class="f-l">去逛逛</p>
                        </div>
                    </a>
                </div>
                </div>
            </div>
            <?php
        }
    } else {
        ?>
        <div class="content-2" style="display: block;">
            <a href="<?= Url::to(['mall-m/login', 'institution_code' => Mall::getCurrentInstitutionCode(), 'return_url' => Yii::$app->request->absoluteUrl]) ?>">
                <div class="log-in">
                    <p class="f-l">立即登录，同步购物车</p>
                    <img class="f-r" src="<?= $web ?>/images/mall-m/a-icon.png"/>
                </div>
            </a>
            <div class="link-btn f-l">
                <a href="<?= Url::to(['mall-m/category', 'institution_code' => Mall::getCurrentInstitutionCode()]) ?>">
                    <div class="stroll f-l">
                        <img class="f-l" src="<?= $web ?>/images/mall-m/stroll-icon.png"/>
                        <p class="f-l">去逛逛</p>
                    </div>
                </a>
                <a style="display: none;">
                    <div class="my-collect f-r">
                        <img class="f-l" src="<?= $web ?>/images/mall-m/collect-icon.png"/>
                        <p class="f-l">我的收藏</p>
                    </div>
                </a>
            </div>

        </div>
        <?php
    }
    ?>


    <div class="guess f-l">
        <div class="left-wire f-l"></div>
        <div class="like f-l">
            <img class="f-l" src="<?= $web ?>/images/mall-m/like-icon.png"/>
            <p class="f-l">猜你喜欢</p>
        </div>
        <div class="right-wire f-r">
        </div>
        <div class="goods-container">
            <div class="goods-grid-frame">
                <?php
                foreach ($hotGoosList as $item) {
                    ?>
                    <a class="goods-item" href="goods-detail?goods_code=<?= $item['code'] ?>&institution_code=<?= $institution_code ?>">
                        <img src="<?= $item['image_url'] ?>@300w">
                        <label class="name crossborder">
                            <?php
                            if($item['is_crossborder']=='1'){
                                ?>
                                <span>跨境</span>
                                <?php
                            }
                            ?>

                            <?= $item['name'] ?></label>
                        <label class="price"><?= $item['integral_unit'] ?>：<?= intval($item['selling_integral']) ?></label>
                    </a>
                    <?php
                }
                ?>
                <!-- <a class="goods-item">
                    <img src="<? /*= $web */ ?>/images/mall-m/1.jpg">
                    <label class="name">呆萌抱抱大肚圣诞陶瓷杯大肚圣圣诞老人</label>
                    <label class="price">积分：1198</label>
                </a>-->
            </div>
        </div>
        <div class="tab-frame-margin-bottom"></div>
    </div>
    <?=MallMBottomToolbarWidget::widget(['actionName'=>'cart'])?>
</div>
<script>
    $(function () {
        var checked_number = 0;
        var input_checkbox = $(".content-text-1 .choice-checkbox");
        $("#all_checkbox").click(function () {
            if ($("#all_checkbox").is(':checked') == true) {
                input_checkbox.prop('checked', true);
            } else {
                $(".content-text-1 .choice-checkbox").prop('checked', false);
            }
        });

        $(input_checkbox).click(function () {


            if ($(this).is(':checked') == false) {
                $("#all_checkbox").prop('checked', false);
            }
            setCheckAllStatus();
        });

        function setCheckAllStatus() {
            checked_number = 0;
            var doms = $(".content-text-1 .choice-checkbox");
            doms.each(function (index, item) {
                if ($(item).prop('checked')) {
                    checked_number += 1;
                }
                if (checked_number == doms.length) {
                    $("#all_checkbox").prop('checked', true);
                }
            });
            calcTotal();
        }

        setCheckAllStatus();
        calcTotal();
    });
    //计算合计
    function calcTotal() {
        var total_price = 0;
        $.each($(".content-text-1"), function (index, item) {
            var check_input = $(item).find('.choice-checkbox');
            if (check_input.is(':checked') == true) {
                var price = $(item).find('.goods-price [name="goods_price"]').html();
                var num = $(item).find('.num').val();
                total_price += parseInt(price) * parseInt(num);
            }
        });
        $('.total [name="totalPrice"]').html(total_price);
        //同步工具栏下面的数量
        refreshToolBarBottomGetCartGoodsCount();
    }
    removeCartGoods = function () {
        var input_checkbox = $(".content-text-1 .choice-checkbox:checked");
        if((input_checkbox.length)>0){
            if (confirm("你确定删除这些商品吗？")==false){
                return;
            }
        }
        $.each(input_checkbox, function (i, item) {
            var data = {};
            data.cartId = $(this).closest('.content-text-1').attr('cartId');
            requestJsonMallM('remove-cart-handle', data);

            //$(item).closest('.content-text-1').remove();
            // $("#all_checkbox").prop('checked', false);
        });
        // calcTotal();
        window.location.reload();
    };
    submit_order = function () {
        var doms = $(".content-text-1 .choice-checkbox:checked");
        if (doms.length <= 0) {
            alert('没有选择需要生成订单的商品!');
            return;
        } else {
            doms = $(".content-text-1 .choice-checkbox");
            $.each(doms, function (i, item) {
                var data = {};
                data.cartId = $(item).closest('.content-text-1').attr('cartId');
                data.isSelected = $(item).is(':checked') ? 1 : 0;
                requestJsonMallM('set-cart-selected-handle', data);
            });
            window.location.href = "<?=Url::to(['mall-m/order-form', 'institution_code' => Mall::getCurrentInstitutionCode()])?>";
        }
    };
</script>
